package com.gopay.bis.bos.ebank.operation;


import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.bos.ebank.BosB2cMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.ICertHelperBos;
import com.koalii.svs.SvsSign;
import com.koalii.svs.SvsVerify;

public class BosB2cCertHelper implements ICertHelperBos{
	
	public String[] signReturnArray(BaseMessage message) throws GopayException {
		try{
			BosB2cMessage messageB2C = (BosB2cMessage)message;
			// 声明
			String[] signAyyat = new String[2];
			if (!messageB2C.isSupportCredit()) {
				// 充值
				signAyyat = rechargeSign(messageB2C);
			} else {
				signAyyat = paySign(messageB2C);
			}
			return signAyyat;
			
		}catch(Exception e){
			BankLogUtils.printB2CSignExceptionLog(message, e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6029);
		}
	}
	public String sign(BaseMessage bsaeMessage) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

	public Object verify(BaseMessage bsaeMessage) throws GopayException {
		BosB2cMessage messageB2C = (BosB2cMessage)bsaeMessage;
		messageB2C.setPlainMessage(messageB2C.getSignDataStr());
		BankLogUtils.printB2CVerifyPlainLog(messageB2C);
		BankLogUtils.printB2CVerifySignatureLog(messageB2C, messageB2C.getSignData());	
		try{
			// 验证数据签名
			int nRet = SvsVerify.verify(messageB2C.getSignDataStr().getBytes(),messageB2C.getSignData(),messageB2C.getPublicKeyPath());
			if (0 == nRet) {
				return true;
			} else {
				return false;
			}
		}catch(Exception e){
			BankLogUtils.printB2CVerifyExceptionLog(messageB2C, e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6031);
		}	
	}
	
	/**
	 * 上海B2C支付交易签名
	 * 
	 * @param Object
	 *            plainText
	 * @return Object
	 * @throws Exception
	 * @throws UnionException
	 */
	public String[] paySign(BosB2cMessage messageB2C) throws Exception {
		SvsSign signer = new SvsSign();
		// 初始化签名证书和私钥
		signer.initSignCertAndKey(messageB2C.getPrivateKeyPath(), messageB2C.getPrivatePwd());
		
		String plainText =  messageB2C.getSignDataStr();
		messageB2C.setPlainMessage(plainText);
		BankLogUtils.printB2CSignPlainLog(messageB2C);
		// 签名结果
		String signDataStr = signer.signData(plainText.getBytes());			
		// 获得签名证书的Base64编码
		String encodedSignCertStr = signer.getEncodedSignCert();
		BankLogUtils.printB2CSignSignatureLog(messageB2C, encodedSignCertStr);
		// 声明
		String[] signAyyat = new String[2];
		signAyyat[0] = signDataStr;
		signAyyat[1] = encodedSignCertStr;
		return signAyyat;

	}

	/**
	 * 上海B2C充值交易签名
	 * 
	 * @param plainText
	 * @return
	 * @throws Exception
	 * @throws UnionException
	 */
	public String[] rechargeSign(BosB2cMessage messageB2C) throws Exception {

		SvsSign signer = new SvsSign();
		// 初始化签名证书和私钥
		signer.initSignCertAndKey(messageB2C.getPrivateKeyPath2nd(), messageB2C.getPrivatePwd2nd());
		
		String plainText =  messageB2C.getSignDataStr();
		messageB2C.setPlainMessage(plainText);
		BankLogUtils.printB2CSignPlainLog(messageB2C);
		// 签名结果
		String signDataStr = signer.signData(plainText.getBytes());			
		// 获得签名证书的Base64编码
		String encodedSignCertStr = signer.getEncodedSignCert();
		BankLogUtils.printB2CSignSignatureLog(messageB2C, encodedSignCertStr);
		// 声明
		String[] signAyyat = new String[2];
		signAyyat[0] = signDataStr;
		signAyyat[1] = encodedSignCertStr;
		return signAyyat;
	}

	
}
